<template>
  <div class="mainLeft" v-show="$route.path==='/home'">
    <ul @click="goSearch">
      <!-- 一级分类 -->
      <li v-for="c in CategoryList" :key="c.categoryId" >
        <a >{{ c.categoryName }}</a>
        <ol>
          <!-- 二级分类 -->
          <li v-for="c1 in c.categoryChild" :key="c1.categoryId">
            <a>{{ c1.categoryName }}<i class="el-icon-arrow-right"></i></a>
            <dl>
              <!-- 三级分类 -->
              <dt v-for="c2 in c1.categoryChild" :key="c2.categoryId">
                <a>{{ c2.categoryName }}</a>
              </dt>
            </dl>
          </li>
        </ol>
      </li>
    </ul>
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: 'LeftNav',
  data () {
    return {

    }
  },
  computed: {
    ...mapState('homeAbout', ['CategoryList'])
  },
  methods: {
    goSearch (e) {
      const a = e.target.outerText
      if (this.$route.path === '/home' || this.$route.name === 'detail') {
        this.$router.push({
          name: 'search',
          query: {
            searchKey: a
          }
        })
      } else {
        this.$bus.$emit('getSearchKey', a)
      }
    }
  },
  mounted () {
    // 向vuex中的actions发送指令，使其获取商品分类，并储存再state
    this.$store.dispatch('homeAbout/getCategoryList')
  }

}
</script>

<style scoped lang='less'>
body{
  overflow: hidden;
}
.mainLeft {
  position: absolute;
  z-index: 9999999999;
  left: 76px;
  float: left;
  width: 210px;
  // height: 500px;
  background-color: #fff;
  ul {
    &>li {
      line-height: 26px;
      transition: 0.5s all;
      display: flex;
      &>a{
        padding-left: 15px;
        flex: 1;
        }
    }
    &>li:hover{
        background-color: pink!important;
      }

     &>li:hover ol {
      display: block;
    }
    ol {
      position: absolute;
      left: 210px;
      top: 0;
      display: none;
      z-index: 9999999999;
      padding-left: 10px !important;
      height: 500px;
      width: 700px;
      background-color: #fff;
      box-shadow: 2px 2px 2px 2px rgba(0, 0, 0, 0.3);
      li {
        overflow: hidden;
        line-height: 30px;
        & > a {
          float: left;
          font-size: 12px;
          font-weight: bold;
        }
        dl {
          padding: 0;
          margin: 0 0 0 5px;
          overflow: hidden;
          dt {
            margin-left: 5px;
            float: left;
            font-size: 13px;
            line-height: 30px;
          }
          dt:hover{
              background-color: dimgrey;
            }
        }
      }
    }
  }
}
</style>
